{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e4e9e39e-86d2-425c-8189-f903aa2283a0",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 28.红外避障传感器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：28.红外避障传感器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：红外避障传感器实验\n",
    "红外避障传感器实验，通过GPIOZero库的Button库直接检测红外避障传感器前方是否有障碍物，然后在jupyter 界面下打印显示提示！！！！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab179391-9c46-48fd-92a4-d95e587c591a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import Button\n",
    "from gpiozero import LED\n",
    "from time import sleep\n",
    "from signal import pause"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.定义红外避障传感器管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_ObstaclePin = Button(17,pull_up=True)      # 红外避障传感器模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 3.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************************\n",
      "* Makerobo Detected Barrier!!!*\n",
      "********************************\n",
      "********************************\n",
      "* Makerobo Detected Barrier!!!*\n",
      "********************************\n",
      "********************************\n",
      "* Makerobo Detected Barrier!!!*\n",
      "********************************\n"
     ]
    }
   ],
   "source": [
    "# 循环函数\t\n",
    "def makerobo_loop():\n",
    "    while True:\n",
    "        if makerobo_ObstaclePin.is_pressed:  # 检测有障碍物\n",
    "            print('********************************') \n",
    "            print('* Makerobo Detected Barrier!!!*') \n",
    "            print('********************************')\n",
    "            sleep(1)\n",
    "\n",
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()        #  调用循环函数\n",
    "\texcept KeyboardInterrupt:  #  当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tpass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
